package uit.shoeshop.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import uit.shoeshop.lib.Constants;

import com.opensymphony.xwork2.ActionSupport;

import java.util.Properties;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class ContactAction extends ActionSupport implements ServletRequestAware,ServletResponseAware{
	private static final long serialVersionUID = 1L;
	private HttpServletRequest request;
	private HttpServletResponse response;

	private String email;
	private String title;
	private String message;
	
	static Properties properties = new Properties();
	static
	{
	      properties.put("mail.smtp.host", "smtp.gmail.com");
	      properties.put("mail.smtp.auth", "true");
	      properties.put("mail.smtp.port", "465");
	      properties.put("mail.smtp.socketFactory.port", "465");
	      properties.put("mail.smtp.socketFactory.class",
	                     "javax.net.ssl.SSLSocketFactory");	      
	}	
	
	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}
	
    public String execute() throws Exception {
    	String ret = NONE;
    	try
    	{
    		if(email != null && title != null && message != null){
	    		Session session = Session.getDefaultInstance(properties,  
	    	            new javax.mail.Authenticator() {
	    	            protected PasswordAuthentication 
	    	            getPasswordAuthentication() {
	    	            return new 
	    	            PasswordAuthentication(Constants.MAIL_ADDRESS, Constants.MAIL_PASS);
	    	            }});
	
	    		 MimeMessage message = new MimeMessage(session);
	    	     message.setFrom(new InternetAddress(Constants.MAIL_ADDRESS));
	    	     message.setRecipients(Message.RecipientType.TO, 
	    	            InternetAddress.parse(Constants.MAIL_CONTACT));
	    	     message.setHeader("Content-Type", "text/plain; charset=UTF-8");
	    	     message.setSubject("[ShoeShop Contact]" + getTitle(),"UTF-8");
	    	     message.setContent(getMessage() + "<br/><b>Liên hệ từ :</b> " + getEmail(),"text/html;charset=UTF-8");
	    	     
	    	     Transport.send(message);
	    	     ret = SUCCESS;
    		}
    	 }catch(Exception e){
    	     ret = ERROR;
    	     e.printStackTrace();
    	 }
        return ret;
    }

	/**
	 * @param email the email to set
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * @return the email
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * @param title the title to set
	 */
	public void setTitle(String title) {
		this.title = title;
	}

	/**
	 * @return the title
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * @param message the message to set
	 */
	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * @return the message
	 */
	public String getMessage() {
		return message;
	}	
}
